package org.example.java.util.concurrency.data_structure;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 *
 * Created by Juntao.Zhang on 2014/11/16.
 */
public class ConcurrentMapDemo {
  static ConcurrentMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
//  static Map<Integer, String> hashMap = (new Hashtable<>());//"Thread-0" java.util.ConcurrentModificationException
  static Map<Integer, String> hashMap = Collections.synchronizedMap(new HashMap<Integer, String>());//"Thread-0" java.util.ConcurrentModificationException

  public static void main(String[] args) {
//    testHashMap();
    testConcurrentHashMap();
  }

  private static void testConcurrentHashMap() {
    final Random random = new Random(47);
    for (int i = 0; i < 100; i++) {
      concurrentHashMap.put(i, "#" + i);
    }

    new Thread() {
      @Override
      public void run() {

        Set<Integer> set = concurrentHashMap.keySet();
        Iterator<Integer> it = set.iterator();
        while (it.hasNext()) {
          int key = it.next();
          System.out.printf("key=%s,value=%s.%n", key, concurrentHashMap.get(key));
          try {
            sleep(Math.abs(random.nextInt(100)));
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();

    new Thread() {
      @Override
      public void run() {
//        Set<Integer> set = concurrentHashMap.keySet();
//        Iterator<Integer> it = set.iterator();
//        while (it.hasNext()) {
//          int key = it.next();
//          concurrentHashMap.put(key,"$"+key);
//          try {
//            sleep(Math.abs(random.nextInt(100)));
//          } catch (InterruptedException e) {
//            e.printStackTrace();
//          }
//        }

        try {
          sleep(200);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        concurrentHashMap.clear();
//        Iterator<Integer> itor = concurrentHashMap.keySet().iterator();
//        while (itor.hasNext()){
//          concurrentHashMap.remove(itor.next());
//        }
        System.out.println("==clear==");
//        try {
//          sleep(20);
//        } catch (InterruptedException e) {
//          e.printStackTrace();
//        }
//        System.out.println("size="+concurrentHashMap.size());
      }
    }.start();
  }

  private static void testHashMap() {
    for (int i = 0; i < 100; i++) {
      hashMap.put(i, "#" + i);
    }

    new Thread() {
      @Override
      public void run() {
        Set<Integer> set = hashMap.keySet();
        Iterator<Integer> it = set.iterator();
        while (it.hasNext()) {
          try {
            sleep(3);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          System.out.println(it.next());
        }
      }
    }.start();

    new Thread() {
      @Override
      public void run() {
        try {
          sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        hashMap.clear();
        System.out.println("==clear==");
      }
    }.start();
  }

}
